package com.sneakxy;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.MissingArgumentException;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

import com.sneakxy.actions.Action;
import com.sneakxy.actions.ActionAlterSplitTable;
import com.sneakxy.actions.ActionCode;
import com.sneakxy.actions.ActionCreate;
import com.sneakxy.actions.ActionDelete;
import com.sneakxy.actions.ActionHelp;
import com.sneakxy.actions.ActionIndex;
import com.sneakxy.actions.ActionSplitTable;

public class Actions {

	public static void main(String[] args) throws Exception {
		Options options = new Options();
		options.addOption("h", "帮助");
		options.addOption("p", "项目生成");
		options.addOption("c", "代码生成");
		options.addOption("t", "模板地址");
		options.addOption(Option.builder("config").hasArg().desc("配置文件地址").build());
		options.addOption("s", "横向分表创建");
		options.addOption("u", "横向分表修改");
		options.addOption("d", "横向分表删除");
		options.addOption("i", "横向分表序号查询");
		
		CommandLineParser parser = new DefaultParser();
		try {
			CommandLine command = parser.parse(options, args, true);
			Action action = null;
			if(command.getOptions().length > 0) {
				if(command.hasOption("p")) {
					//创建项目
					action = new ActionCreate();
				} else if(command.hasOption("c")) {
					//代码生成
					action = new ActionCode();
				} else if(command.hasOption("h")) {
					//帮助
					action = new ActionHelp();
				} else if(command.hasOption("s")) {
					//创建分表
					action = new ActionSplitTable();
				} else if(command.hasOption("u")) {
					//修改分表
					action = new ActionAlterSplitTable();
				} else if(command.hasOption("d")) {
					//删除分表
					action = new ActionDelete();
				} else if(command.hasOption("i")) {
					//分表序号
					action = new ActionIndex();
				}
			} else {
				action = new ActionHelp();
			}
			action.execute(command, options);
		} catch(MissingArgumentException | MissingOptionException e ) {
			System.out.println(e.getLocalizedMessage());
			new ActionHelp().execute(null, options);
		}
		
	}
	
}
